sap.ui.define([
    "sap/ui/core/mvc/Controller",
    "sap/m/MessageBox",
    "sap/m/MessageToast",
    'sap/m/MessagePopover',
    'sap/m/MessageItem',
    'sap/m/Link',
    "sap/ui/model/json/JSONModel",
    'sap/m/MessageView',
    'sap/m/Popover',
    'sap/ui/core/IconPool',
],
    /**
     * @param {typeof sap.ui.core.mvc.Controller} Controller
     */
    function (Controller, MessageBox, MessageToast, MessagePopover, MessageItem, Link, JSONModel, MessageView, Popover,IconPool) {
        "use strict";
        var oMessagePopover;

        return Controller.extend("c100.controller.Main", {
            onInit: function () {
                var that = this

                var sErrorDescription = "<h2>Heading h2</h2>" +
                    "<p>Paragraph. At vero eos et accusamus et iusto odio dignissimos ducimus qui ...</p>" +
                    "<ul>" +
                    "	<li>Unordered list item 1 <a href=\"http://sap.com/some/url\">Absolute URL that is disabled after validation.</a></li>" +
                    "	<li>Unordered list item 2</li>" +
                    "</ul>" +
                    "<ol>" +
                    "	<li>Ordered list item 1 <a href=\"#\">Relative URL that is allowed after validation.</a></li>" +
                    "	<li>Ordered list item 2</li>" +
                    "</ol>";

                var oLink = new Link({
                    text: "Show more information",
                    href: "http://sap.com",
                    target: "_blank"
                });

                var oMessageTemplate = new MessageItem({
                    type: '{msg>type}',
                    title: '{msg>title}',
                    activeTitle: "{msg>active}",
                    description: '{msg>description}',
                    subtitle: '{msg>subtitle}',
                    counter: '{msg>counter}',
                    link: oLink,
                    markupDescription: true
                });

                oMessagePopover = new MessagePopover({
                    items: {
                        path: 'msg>/',
                        template: oMessageTemplate
                    },
                    activeTitlePress: function () {
                        MessageToast.show('Active title is pressed');
                    }
                });


                this.oMessageView = new MessageView({
                    showDetailsPageHeader: false,
                    itemSelect: function () {
                        oBackButton.setVisible(true);
                    },
                    items: {
                        path: "msg>/",
                        template: oMessageTemplate
                    }
                });

                var oBackButton = new sap.m.Button({
					icon: IconPool.getIconURI("nav-back"),
					visible: false,
					press: function () {
						that.oMessageView.navigateBack();
						that._oPopover.focus();
						this.setVisible(false);
					}
				});

                var oCloseButton = new sap.m.Button({
                    text: "Close",
                    press: function () {
                        this._oPopover.close();
                    }.bind(this)
                }).addStyleClass("sapUiTinyMarginEnd"),
                    oPopoverFooter = new sap.m.Bar({
                        contentRight: oCloseButton
                    }),
                    oPopoverBar = new sap.m.Bar({
                        contentLeft: [oBackButton],
                        contentMiddle: [
                            new sap.m.Title({ text: "Messages" })
                        ]
                    });

                this._oPopover = new sap.m.Popover({
                    customHeader: oPopoverBar,
                    contentWidth: "440px",
                    contentHeight: "440px",
                    verticalScrolling: false,
                    modal: true,
                    content: [this.oMessageView],
                    footer: oPopoverFooter
                });

                var aMockMessages = [{
                    type: 'Error',
                    title: 'Error message',
                    active: true,
                    description: sErrorDescription,
                    subtitle: 'Example of subtitle',
                    counter: 1
                }, {
                    type: 'Warning',
                    title: 'Warning without description',
                    description: ''
                }, {
                    type: 'Success',
                    title: 'Success message',
                    description: 'First Success message description',
                    subtitle: 'Example of subtitle',
                    counter: 1
                }, {
                    type: 'Error',
                    title: 'Error message',
                    description: 'Second Error message description',
                    subtitle: 'Example of subtitle',
                    counter: 2
                }, {
                    type: 'Information',
                    title: 'Information message',
                    description: 'First Information message description',
                    subtitle: 'Example of subtitle',
                    counter: 1
                }];

                this.getView().setModel(new JSONModel(aMockMessages), "msg");
                this.byId("messagePopoverBtn").addDependent(oMessagePopover);
                this.oMessageView.setModel(new JSONModel(aMockMessages), "msg");

            },
            buttonIconFormatter: function () {
                return "sap-icon://error";
            },
            buttonTypeFormatter: function () {
                return "Success";
            },
            highestSeverityMessages: function () {
                return "22"
            },
            handleMessagePopoverPress: function (oEvent) {
                oMessagePopover.toggle(oEvent.getSource(),null,sap.m.PlacementType.Left);
            },
            handlePopoverPress: function (oEvent) {
                // this._oPopover.setPlacement(sap.m.PlacementType.Top);
                this._oPopover.openBy(oEvent.getSource());
            },
            handleDialogPress: function (oEvent) {

                var oBackButton = new sap.m.Button({
					icon: IconPool.getIconURI("nav-back"),
					visible: false,
					press: function () {
						that.oMessageView.navigateBack();
						this.setVisible(false);
					}
				});

                this.oDialog = new sap.m.Dialog({
                    resizable: true,
                    content: this.oMessageView,
                    state: 'Error',
                    beginButton: new sap.m.Button({
                        press: function () {
                            this.getParent().close();
                        },
                        text: "Close"
                    }),
                    customHeader: new sap.m.Bar({
                        contentLeft: [oBackButton],
                        contentMiddle: [
                            new sap.m.Title({
                                text: "Error",
                                level: 'H1'
                            })
                        ]
                    }),
                    contentHeight: "50%",
                    contentWidth: "50%",
                    verticalScrolling: false
                });

                this.oDialog.open();
            },
            onConfirmationMessageBoxPress: function () {
                MessageBox.confirm("Approve purchase order 12345?");
            },

            onAlertMessageBoxPress: function () {
                MessageBox.alert("The quantity you have reported exceeds the quantity planed.");
            },

            onErrorMessageBoxPress: function () {
                MessageBox.error("Select a team in the \"Development\" area.\n\"Marketing\" isn't assigned to this area.");
            },

            onInfoMessageBoxPress: function () {
                MessageBox.information("Your booking will be reserved for 24 hours.");
            },

            onWarningMessageBoxPress: function () {
                MessageBox.warning("The project schedule was last updated over a year ago.");
            },

            onSuccessMessageBoxPress: function () {
                MessageBox.success("Project 1234567 was created and assigned to team \"ABC\".");
            },
            onError2MessageBoxPress: function () {
                MessageBox.error("Product A does not exist.", {
                    actions: ["Manage Products", MessageBox.Action.OK, MessageBox.Action.CLOSE],
                    emphasizedAction: "Manage Products",
                    initialFocus: "Manage Products",
                    details: "<p><strong>This can happen if:</strong></p>" +
                        "<ul>" +
                        "<li>You are not connected to the internet</li>" +
                        "<li>a backend component is not <em>available</em></li>" +
                        "<li>or an underlying system is down</li>" +
                        "</ul>" +
                        "<p>Get more help <a href='//www.sap.com' target='_top'>here</a>.",
                    onClose: function (sAction) {
                        sap.m.MessageToast.show("Action selected: " + sAction);
                    }
                });
            },
            onMessageToastPress:function(){
                MessageToast.show("Message Toast Clicked");
            }
        });
    });
